/*
$scope.scriptObj = {
        obj: {},  //小节对象
        isShow:false   //是否显示
    }

   脚本格式
   [{
   title:"标题",
   type:"类型" 1提问  2知识点
   contents:[{
   text:""
   toTime: // 跳转到
   }],
   content:"" // 知识点
   time:
   }]
*/
angular.module('courseScript',["wangeditordrective","Common"])
.directive("courseScript",["$rootScope", "sysUserInfo", "Msg","MsgCon", "T","Comm", function ($rootScope, sysUserInfo, Msg, MsgCon, T, Comm) {
	return {
         restrice: "AE",
         scope: {             
             scriptObj: "=",      //关闭窗口                    
         },
         templateUrl: "/system/directive/train/coursescript/course_script.html",
         link: function (scope, element, attr) {
            // 取出章节                
            scope.sObj = scope.scriptObj.obj;                
            if(Comm.isNull(scope.scriptObj.obj.SCRIPTCONTENT)){
                scope.scriptObj.obj.SCRIPTCONTENT = [];
            }
            scope.scriptContent = scope.scriptObj.obj.SCRIPTCONTENT;         
            //if (!(scope.scriptContent instanceof Array)) {
            //    scope.scriptContent = JSON.parse(scope.scriptObj.obj.SCRIPTCONTENT);                
            //}                                   
            // 脚本对象
            scope.nObj = {};            
            // 初始化播放器          
            scope.initPlayer = function(url){
                scope.player = cyberplayer("VideoPlayer").setup({
                    width:'100%',
                    height:'100%',
                    backcolor : "#FFFFFF",                
                    autostart: true,
                    stretching: "uniform",
                    volume: 100,                
                    controlbar:{
                        barLogo: false
                    },                    
                    repeat : false,
                    file: url, // 播放地址
                    ak: "7200a73f5c7042b9a1cddd897de56bb3" // 公有云平台注册即可获得accessKey
                });
            }            
            // 初始化
            scope.init = function(){                            
                if(!Comm.isNull(scope.sObj)){
                    // 处理视频地址                                                            
                    try{
                        scope.initPlayer(scope.sObj.CSURLNAME.substr(0,scope.sObj.CSURLNAME.indexOf("?")) +"?userid="+sysUserInfo.getuserinfo().user_ID+"&orgid="+sysUserInfo.getuserinfo().organization_ID);
                    }catch(e){
                        scope.initPlayer(scope.sObj.CSURLNAME);
                    }                              
                    // 处理脚本
                    if(!Comm.isNull(scope.scriptContent)){         
                        for (var i = 0; i < scope.scriptContent.length; i++) {
                           delete scope.scriptContent[i].flag;
                        }               
                        // 脚本处理                                        
                        scope.scriptContent[0].flag = true;
                        scope.nObj = scope.scriptContent[0];
                    }               
                }                                
                console.log(scope.scriptObj.obj);
            }
            // 切换类型
            scope.handType = function(obj,type){
                // 为了防止乱所以每个脚本对象作为参数传递，这里可以直接用scope.nObj
                obj.type = type;
                
                // 提问
                if(type == 1){
                    delete obj.content                
                    obj.contents = [];
                    for (var i = 0; i < 4; i++) {
                        //"idx":i,
                        obj.contents.push({"text":"","toTime":0});
                    }
                }else{
                    delete obj.contents;
                    obj.content = "";
                }
            }
            // 添加选项
            scope.addOption = function(list){
                list.push({"text":"","toTime":0});
            }
            // 删除选项
            scope.delOption = function(list,item){
                for (var i = 0; i < list.length; i++) {
                    if(list[i] == item){
                        list.splice(i,1);
                    }
                }
            }
            // 选中脚本
            scope.selScript = function(sNode){                
                // 视频跳转
                scope.player.seek(sNode.time);
                setTimeout(function(){
                    if(scope.player.getState() == "playing"){
                        // 暂停
                        scope.player.pause();                    
                    }
                },300);                
                // 脚本处理                
                for (var i = 0; i < scope.scriptContent.length; i++) {
                    delete scope.scriptContent[i].flag 
                }
                sNode.flag = true;
                scope.nObj = sNode;
            }
            // 添加脚本 
            scope.addScript = function(){
                // 脚本处理                
                for (var i = 0; i < scope.scriptContent.length; i++) {
                    delete scope.scriptContent[i].flag;
                }
                scope.scriptContent.push({"title":"", "type":1,"content":"","time":parseInt(scope.player.getPosition()),flag:true});
                scope.nObj = scope.scriptContent[scope.scriptContent.length-1];
                scope.handType(scope.nObj,1);
                // 暂停
                if(scope.player.getState() == "playing"){
                    scope.player.pause();    
                }                
            }    
            // 删除脚本
            scope.delScript = function(){
                MsgCon.delnoicon().then(function (flag) { 
                    if(flag){
                        var index = -1;
                        scope.nObj = {};
                        for (var i = 0; i < scope.scriptContent.length; i++) {
                            if(scope.scriptContent[i].flag){
                                scope.scriptContent.splice(i,1);
                                index = i-1;
                            } 
                        }
                        // 选中最后一个
                        if(index != -1 && scope.scriptContent.length > 0){
                            scope.scriptContent[index].flag = true;
                            scope.nObj = scope.scriptContent[index]
                        } 
                    }                
               });                                               
            }        
            //配置富文本编辑器的按钮
            scope.configbtnzhishi = [ 'bold', 'underline', 'italic', 'strikethrough', 'eraser', 'forecolor', 'img', 'quote', 'fontsize', 'head', 'alignleft', 'aligncenter', 'alignright'];
            // 关闭
            scope.onClose = function(){
                // 对JSON 进行处理
                scope.scriptObj.isShow = false;                
            }
            // 初始化
            scope.init();                        
         }
     }
}]);